package tongcheng.com.tongcheng.ui.yemian.shenfen.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.yanzhenjie.nohttp.FileBinary;
import com.yanzhenjie.nohttp.RequestMethod;

import java.io.File;
import java.util.List;

import butterknife.Bind;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.base.BaseActivity;
import tongcheng.com.tongcheng.bean.StatusBean;
import tongcheng.com.tongcheng.bean.UpImageBean;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.utils.GsonUtil;
import tongcheng.com.tongcheng.utils.Setting;
import tongcheng.com.tongcheng.utils.ShowToast;
import tongcheng.com.tongcheng.widght.PhotoPopWindow;

public class InstallUpLoadPic extends BaseActivity {

    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.installup_ll1)
    LinearLayout mInstallupLl1;
    @Bind(R.id.installup_next)
    Button mInstallupNext;
    @Bind(R.id.back_text)
    TextView mBackText;
    @Bind(R.id.title_bar)
    RelativeLayout mBg;

    @Bind(R.id.installup_pic1)
    ImageButton mInstallupPic1;
    @Bind(R.id.installup_pic2)
    ImageButton mInstallupPic2;
    @Bind(R.id.installup_pic3)
    ImageButton mInstallupPic3;
    @Bind(R.id.installup_pic4)
    ImageButton mInstallupPic4;

    public int temp = 0;
    private List<LocalMedia> selectList;

    @Override
    public Integer initResid() {
        return R.layout.activity_install_up_load_pic;
    }

    String Url1 = "";
    String Url2 = "";
    String Url3 = "";
    String Url4 = "";

    @Override
    public void initCreate(Bundle savedInstanceState) {
        settitlebar(mBg);
        mTitle.setText("上传图片");
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mInstallupNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (TextUtils.isEmpty(Url1) || TextUtils.isEmpty(Url2) || TextUtils.isEmpty(Url3) ) {
                    ShowToast.showToast("请把图片上传完整");
                } else {
                    if (getIntent().getStringExtra("temp") != null) {
                        DailiUp();
                    } else {
                        InstallUp();
                    }
                }




               // startactivity(InstallUpLoadPic.this, BaoZhenJinActivity.class);
            }


        });


        mInstallupPic1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(Url1)) {
                    temp = 1;
                    PhotoPopWindow PhotoPopWindow = new PhotoPopWindow(InstallUpLoadPic.this,InstallUpLoadPic.this);
                    PhotoPopWindow.show(mBg);
                }

            }
        });


        mInstallupPic2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(Url2)) {
                    temp = 2;
                    PhotoPopWindow PhotoPopWindow = new PhotoPopWindow(InstallUpLoadPic.this,InstallUpLoadPic.this);
                    PhotoPopWindow.show(mBg);
                }

            }
        });

        mInstallupPic3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(Url3)) {
                    temp = 3;
                    PhotoPopWindow PhotoPopWindow = new PhotoPopWindow(InstallUpLoadPic.this,InstallUpLoadPic.this);
                    PhotoPopWindow.show(mBg);
                }

            }
        });


        mInstallupPic4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(Url4)) {
                    temp = 4;
                    PhotoPopWindow PhotoPopWindow = new PhotoPopWindow(InstallUpLoadPic.this,InstallUpLoadPic.this);
                    PhotoPopWindow.show(mBg);
                }
            }
        });
    }
    //安装人员完善资料
    private void InstallUp() {
        MyRequest request = new MyRequest(ConStans.UpChangInfo_Url, RequestMethod.POST);
        request.add("type", 5);
        request.add("phone", Setting.getPhone());
        request.add("user_name", Setting.getUserName());
        request.add("address", Setting.getAddress());
        request.add("idno", Setting.getShenFenNo());
        request.add("tel", Setting.getLianXiDianHua());
//        request.add("serve_type", Setting.getFuwuLeixing1());
//        request.add("serve_area", Setting.getFuwuArea1());
        request.add("suf_pic", Url1 + "," + Url2 + "," + Url3 + "," + Url4 + ",");
        CallServer.getInstance().addPost(this,request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                StatusBean statusBean = GsonUtil.GsonToBean(json, StatusBean.class);
                if (statusBean != null) {
                    if (statusBean.msg != null) {
                        ShowToast.showToast(statusBean.msg);
                    }
                    if (statusBean.status == 1) {
                        Setting.setTemp(0);
                        startactivity(InstallUpLoadPic.this, BaoZhenJinActivity.class);
                    }
                }
            }
        });
    }
    //代理商完善资料
    private void DailiUp() {
        MyRequest request = new MyRequest(ConStans.UpChangInfo_Url, RequestMethod.POST);
        request.add("type", 3);
        request.add("phone", Setting.getPhone());
        request.add("user_name", Setting.getUserName());
        request.add("address", Setting.getArea());
        request.add("address_detail", Setting.getAddress());
        request.add("scale", Setting.getGuiMo());
        request.add("record", Setting.getZiLi());
        request.add("documents_features", Setting.getZhenShu());
//        request.add("serve_type", Setting.getFuwuLeixing1());
//        request.add("serve_area", Setting.getFuwuArea1());
        request.add("suf_pic", Url1 + "," + Url2 + "," + Url3 + "," + Url4 + ",");
       CallServer.getInstance().addPost(this,request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                StatusBean statusBean = GsonUtil.GsonToBean(json, StatusBean.class);
                if (statusBean != null) {
                    if (statusBean.msg != null) {
                        ShowToast.showToast(statusBean.msg);
                    }
                    if (statusBean.status == 1) {
                        Setting.setTemp(0);
                        startactivity(InstallUpLoadPic.this, BaoZhenJinActivity.class);
                    }
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    selectList = PictureSelector.obtainMultipleResult(data);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    // ShowToast.showToast("我被回调了");
                /*    for (LocalMedia localMedia : selectList) {
                        Log.e("tag", localMedia.getPath());
                    }*/
                    UpImage(selectList);

                    break;
            }
        }
    }
    //上传图片
    private void UpImage(List<LocalMedia> selectList) {
        MyRequest request = new MyRequest(ConStans.UpImage_Url, RequestMethod.POST);
        request.add("code", "image");
        request.add("num", selectList.size());
        boolean bb = selectList.get(0).isCompressed();
        if(bb){
            request.add("image" + 1, new FileBinary(new File(selectList.get(0).getCompressPath())));
        }else {
            request.add("image" + 1, new FileBinary(new File(selectList.get(0).getPath())));
        }

        CallServer.getInstance().addPost(this,request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                UpImageBean upImageBean = GsonUtil.GsonToBean(json, UpImageBean.class);
                if (upImageBean != null) {
                    switch (temp) {
                        case 1:
                            //TODO 加载第一张图片
                            Url1 = upImageBean.data.get(0).filename;
                            mInstallupPic1.setBackgroundColor(Color.TRANSPARENT);
                            Glide.with(InstallUpLoadPic.this).load(ConStans.BaseUrl_Test_url+upImageBean.data.get(0).filename).into(mInstallupPic1);
                          //  mInstallupPic1.setImageURI(Uri.fromFile(new File(selectList.get(0).getPath())));
                            break;
                        case 2:
                            Url2 = upImageBean.data.get(0).filename;
                            mInstallupPic2.setBackgroundColor(Color.TRANSPARENT);
                            Glide.with(InstallUpLoadPic.this).load(ConStans.BaseUrl_Test_url+upImageBean.data.get(0).filename).into(mInstallupPic2);
                            //TODO 加载第二张图片

                            break;
                        case 3:
                            Url3 = upImageBean.data.get(0).filename;
                            mInstallupPic3.setBackgroundColor(Color.TRANSPARENT);
                            Glide.with(InstallUpLoadPic.this).load(ConStans.BaseUrl_Test_url+upImageBean.data.get(0).filename).into(mInstallupPic3);
                            //TODO 加载第三张图片

                            break;
                        case 4:
                            Url4 = upImageBean.data.get(0).filename;
                            mInstallupPic4.setBackgroundColor(Color.TRANSPARENT);
                            Glide.with(InstallUpLoadPic.this).load(ConStans.BaseUrl_Test_url+upImageBean.data.get(0).filename).into(mInstallupPic4);
                            //TODO 加载第四张图片
                            /*mInstallupPic4.setBackgroundColor(Color.TRANSPARENT);
                            mInstallupPic4.setImageURI(Uri.fromFile(new File(selectList.get(0).getPath())));*/
                            break;
                    }


                }
            }
        });

    }

}
